package com.admin.mds.system.controller;

import org.springframework.web.bind.annotation.RestController;

import com.admin.mds.system.model.entity.SysRoleEntity;
import com.admin.mds.system.service.SysRoleService;
import com.admin.utils.base.BaseController;
import com.admin.utils.base.DataResult;
import com.admin.utils.base.page.pageFactory;
import com.admin.utils.constant.UserUtil;
import com.admin.utils.util.EasyExcelToRequestUtil;
import com.admin.utils.util.ValidResult;
import com.admin.utils.util.ValidationUtils;
import com.github.pagehelper.PageInfo;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;



import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

/**
 * 角色信息表Controller
 * 
 * @author monxz
 * @version 2021-01-29
 */
@RestController
@RequestMapping(value = "user/sysRole")
@Api(tags = "角色信息表")
public class SysRoleController extends BaseController {

	@Autowired
	private SysRoleService sysRoleService;

	@PostMapping(value = { "/list" })
	public DataResult list(@RequestBody SysRoleEntity sysRolePojo) throws Exception {
		pageFactory.startPage();
		PageInfo<SysRoleEntity> page = new PageInfo<>(sysRoleService.findSysRoleList(sysRolePojo));
		return DataResult.buildSuccess(page);
	}

	@PostMapping(value = "/save")
	public DataResult save(@RequestBody SysRoleEntity sysRolePojo) throws Exception {
		// 校验接口参数
		if (sysRolePojo.getRoleSort() <= UserUtil.getUserMaxRoleLevel(null)) {
			return DataResult.buildFail("不得修改角色级别高于您的数据");
		}

		// 校验接口参数
		ValidResult validResult = ValidationUtils.validateGroup(sysRolePojo);
		if (validResult.isHasErrors()) {
			return DataResult.buildFail(validResult.getErrorMsg());
		}
		
		SysRoleEntity  ckRole = new SysRoleEntity();
		ckRole.setRoleName(sysRolePojo.getRoleName());
		if(sysRoleService.checkUnion(ckRole) == 1) {
			return DataResult.buildFail("角色名称重复");
		}
		return DataResult.buildByInter(sysRoleService.addSysRole(sysRolePojo));
	}

	@PostMapping(value = "/update")
	public DataResult update(@RequestBody SysRoleEntity sysRolePojo) throws Exception {
		// 主键判断
		if (sysRolePojo.getRoleId() == null) {
			return DataResult.buildFail("没有对应的Id");
		}

		// 级别判断
		if (sysRolePojo.getRoleSort() < UserUtil.getUserMaxRoleLevel(null)) {
			return DataResult.buildFail("不得修改角色级别高于您的数据");
		}
		

		// 校验接口参数
		ValidResult validResult = ValidationUtils.validateGroup(sysRolePojo);
		if (validResult.isHasErrors()) {
			return DataResult.buildFail(validResult.getErrorMsg());
		}
		
		SysRoleEntity  ckRole = new SysRoleEntity();
		ckRole.setRoleName(sysRolePojo.getRoleName());
		ckRole.setRoleId(sysRolePojo.getRoleId());
		if(sysRoleService.checkUnion(ckRole) == 1) {
			return DataResult.buildFail("角色名称重复");
		}

		return DataResult.buildByInter(sysRoleService.modifySysRole(sysRolePojo));

	}

	@PostMapping(value = "/delete")
	public DataResult delete(@RequestBody String ids) throws Exception {
		if (ids == null || ids.trim().length() == 0) {
			return DataResult.buildFail("参数不能为空");
		}
		return DataResult.buildByInter(sysRoleService.deleteFalse(ids));
	}

	@GetMapping(value = "/excel")
	@ApiOperation("用户导出")
	public void excel(SysRoleEntity role, HttpServletResponse req) throws Exception {
		List<SysRoleEntity> roleList = sysRoleService.findSysRoleList(role);
		Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("角色信息", "角色信息"), SysRoleEntity.class,
				roleList);
		String fileName = "用户列表（" + new SimpleDateFormat("yyyy-MM-dd").format(new Date()) + ").xls";
		EasyExcelToRequestUtil.reqExcel(req, fileName, workbook);
	}

}